package com.app.controller;

import com.app.common.EducationTool;
import com.app.common.IndustryTool;
import com.app.common.ScaleTool;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ScaleVo;
import com.app.vo.SkillTrainingVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Controller
@RestController
public class SkillTrainingController {

    @Autowired
    UserService userService;
    @Autowired
    SkillTrainingService skillTrainingService;
    @Autowired
    TrainTypeConfigService trainTypeConfigService;

    @Autowired
    TrainTypeConfigSonService trainTypeConfigSonService;
    @Autowired
    ConfigService configService;

    /**
     * 查询培训列表
     */
    @GetMapping("/getTrainingList")
    public Result<Object> getTrainingList(HttpServletRequest request) throws ParseException {
        String state = request.getParameter("state");
        int page = Integer.valueOf(request.getParameter("page"));
        int limit = Integer.valueOf(request.getParameter("limit"));
        SkillTrainingModel model = new SkillTrainingModel();
        model.setPage(page);
        model.setLimit(limit);
        if (state != null && state != "") {
            model.setState(Integer.valueOf(state));
        }
        int count = skillTrainingService.getSkillTrainingCount(model);
        Page<SkillTrainingModel> modelList = skillTrainingService.getSkillTrainingList(model);


        SkillTrainingVo vo = new SkillTrainingVo();
        vo.setCount(count);
        vo.setModelList(modelList.getRecords());
        return Result.success(vo);
    }
    //根据ID查询
    @GetMapping("/getTraining")
    public Result<Object> getTraining(HttpServletRequest request){
        String id = request.getParameter("id");
        SkillTrainingModel model = skillTrainingService.getSkillTrainingById(Integer.valueOf(id));
        List<TrainTypeConfigModel> trainTypeConfigModelList = trainTypeConfigService.getListById(model.getId());
        if(trainTypeConfigModelList!=null)
        {
            for (int i = 0; i < trainTypeConfigModelList.size(); i++) {
                List<TrainTypeConfigSonModel> trainTypeConfigSonModelList = trainTypeConfigSonService.getListByTypeId(trainTypeConfigModelList.get(i).getType());
                //获取一级分类名称
                ConfigModel configModel  = configService.getModelById(trainTypeConfigModelList.get(i).getType());
                trainTypeConfigModelList.get(i).setTypeText(configModel.getName());
                for (int j = 0; j < trainTypeConfigSonModelList.size(); j++) {
                    //获取专业名称
                    ConfigModel configModel2 = configService.getModelById(trainTypeConfigSonModelList.get(j).getConfigId());
                    trainTypeConfigSonModelList.get(j).setTrainIdText(model.getCompany());
                    trainTypeConfigSonModelList.get(j).setConfigIdText(configModel2.getName());
                    trainTypeConfigSonModelList.get(j).setTypeIdText(configModel.getName());
                }
                trainTypeConfigModelList.get(i).setSonList(trainTypeConfigSonModelList);
            }
            model.setTypeConfigModelList(trainTypeConfigModelList);
        }
        if (model != null) {
            return Result.success(model);
        }
        return Result.success(null);
    }

    @GetMapping("/getTrainSpecialityById")
    public Result<Object> getTrainSpecialityById(Integer id){
        TrainTypeConfigSonModel model = trainTypeConfigSonService.getModelById(id);
        ConfigModel configModel2 = configService.getModelById(model.getConfigId());
        model.setConfigIdText(configModel2.getName());
        return Result.success(model);
    }

}
